
import win32api
import win32service
import win32con
import sys
import os
from stat import ST_ATIME, ST_MTIME # Really constants 7 and 8

def EnumServices():
    resume = 0
    accessSCM = win32con.GENERIC_READ
    accessSrv = win32service.SC_MANAGER_ALL_ACCESS

    #Open Service Control Manager
    hscm = win32service.OpenSCManager(None, None, accessSCM)

    #Enumerate Service Control Manager DB
    typeFilter = win32service.SERVICE_WIN32
    stateFilter = win32service.SERVICE_STATE_ALL

    statuses = win32service.EnumServicesStatus(hscm, typeFilter, stateFilter)
    for (short_name, desc, status) in statuses:
        print short_name, desc, status

def ShowStat(inpFile):
    try:
        fileStat = os.stat(inpFile)
    except os.error:
        sys.stderr.write(inpFile + ': cannot stat\n')
        sys.exit(1)
    print fileStat

def GetFileVersion():
    ver_strings=('Comments','InternalName','ProductName', 
        'CompanyName','LegalCopyright','ProductVersion', 
        'FileDescription','LegalTrademarks','PrivateBuild', 
        'FileVersion','OriginalFilename','SpecialBuild')
    fname = os.environ["comspec"]
    d=win32api.GetFileVersionInfo(fname, '\\')
    ## backslash as parm returns dictionary of numeric info corresponding to VS_FIXEDFILEINFO struc
    for n, v in d.items():
        print n, v

    pairs=win32api.GetFileVersionInfo(fname, '\\VarFileInfo\\Translation')
    ## \VarFileInfo\Translation returns list of available (language, codepage) pairs that can be used to retreive string info
    ## any other must be of the form \StringfileInfo\%04X%04X\parm_name, middle two are language/codepage pair returned from above
    for lang, codepage in pairs:
        print 'lang: ', lang, 'codepage:', codepage
        for ver_string in ver_strings:
            str_info=u'\\StringFileInfo\\%04X%04X\\%s' %(lang,codepage,ver_string)
            ## print str_info
            print unicode(ver_string), unicode(win32api.GetFileVersionInfo(fname, str_info))

if __name__ == '__main__':
    ShowStat(sys.argv[0])
    GetFileVersion()
    EnumServices()
